/*
 * Cellblock general-purpose API wrapper, a component of the 
 * Gloto Platform.  See www.gloto.com for more information,
 * terms, and licensing.
 *
 * Copyright (C) 2005-2008, Gloto Corp.
 * http://www.cellblock.com/terms.htm
 */
 
/*****************************
 **                         **
 **     Utility Methods     **
 **                         **
 *****************************/

var _interval = 30000; // Interval in milliseconds; Default 30 seconds
var _cellblocks = new Array();
var _apiKey = 'http://www.yeahyeahyeahyeahyeah.com'

function AddNewFrameListener(cellblock, callback){
	FrameInfoForCellblock(_apiKey, cellblock, 0, 1, function(data){
		BuildCellblockObject(cellblock, callback);
		SetFirstFrame(cellblock, data);
		CheckForNewFrames(cellblock); });	
}

function BuildCellblockObject(cellblock, callback){
	_cellblocks[cellblock] = new Object();
		
	_cellblocks[cellblock].callback = callback;
}

function CheckForNewFrames(cellblock){
	var GetNewFrames = function(data){
		if( data.status == "ok" && data.frames.length > 0 ){
			SetFirstFrame(cellblock, data);
			_cellblocks[cellblock].callback(data.frames);	
		}
	}
	
	if(_cellblocks[cellblock].wIndex != null){
		GetFramesNewerThan(_apiKey, cellblock, _cellblocks[cellblock].wIndex, GetNewFrames);
	}
	
	setTimeout("CheckForNewFrames('" + cellblock + "')", _interval);
}

function SetFirstFrame(cellblock, data){
	if(data.status == "ok"){
		var i = data.frames.length - 1;
		var frame = data.frames[i];

		if(frame != null && frame.wIndex != null){
			_cellblocks[cellblock].wIndex = frame.wIndex;
		}else{
			_cellblocks[cellblock].wIndex = 0;
		}
	}
}

function SetNewFrameListenerInterval(interval){
	_interval = interval;
}
